<?php
/**
 * 插件相关
 *
 * PHP Version 5.6
 *
 * @category PHP
 * @package  Telegram
 * @author   ctwj <908504609@qq.com>
 * @license  MIT https://github.com/ctwj/wechat_search/blob/master/LICENSE
 * @link     https://github.com/ctwj/wechat_search/
 * @date     2019-01-02 23:56:47
 * @modifyby ctwj
 */

namespace Telegram;

class Plugins
{
    /**
     * 注册消息监听
     *
     * @param [Symfony\Component\EventDispatcher\EventDispatcher] $dispatcher dispatcher
     *
     * @return void
     */
    public static function registerPlugin($dispatcher)
    {
        $plugins = [];
        $dir = new \DirectoryIterator(PLUGINS_PATH);
        foreach ($dir as $fileInfo) {
            if ($fileInfo->isDot()) {
                continue;
            }
            $filename = $fileInfo->getFilename();
            $info = explode('.', $filename);
            if (count($info) != 2 || $info[1] != 'php' || $info[0] == 'BasePlugin') {
                continue;
            }
            $plugins[] = "\\Telegram\\Plugins\\" . $info[0];
        }
        
        if (!empty($plugins)) {
            foreach ($plugins as $plugin) {
                $plugin::registerListener($dispatcher);
            }
        }
    }
}
